/**
 * Created by 白雨浓 on 2018/1/23 17:53.
 */

import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default {
    readTmpFile(self, name){
        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, `${name}.ejs`);
        if (!fs.existsSync(filePath)) {
            self.$$msg.error(`${name}模板文件缺失`);
            return;
        }
        return fs.readFileSync(filePath, 'utf-8');
    },
    select(self, name, fields){
        const tmpFile = this.readTmpFile(self, 'select');
        if (!tmpFile) return;

        let clsName = self.$str.toCamel(name);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        let variables = [];

        const debars = ['id', 'state', 'update_time', 'create_time'];

        fields.forEach(field => {
            if (debars.indexOf(field.COLUMN_NAME) < 0) {
                variables.push({
                    jName: self.$str.toCamel(field.COLUMN_NAME),
                    sName: field.COLUMN_NAME
                });
            }
        });

        const tmp = {
            tableName: name,
            variables: variables
        };

        return ejs.render(tmpFile, tmp);
    },
    selectList(self, name, fields){
        const tmpFile = this.readTmpFile(self, 'selectList');
        if (!tmpFile) return;

        let clsName = self.$str.toCamel(name);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        let variables = [];

        const debars = ['id', 'state', 'update_time', 'create_time'];

        fields.forEach(field => {
            if (debars.indexOf(field.COLUMN_NAME) < 0) {
                variables.push({
                    jName: self.$str.toCamel(field.COLUMN_NAME),
                    sName: field.COLUMN_NAME
                });
            }
        });

        const tmp = {
            tableName: name,
            variables: variables
        };

        return ejs.render(tmpFile, tmp);
    },
    selectPage(self, name, fields){
        const tmpFile = this.readTmpFile(self, 'selectPage');
        if (!tmpFile) return;

        let variables = [];

        const debars = ['id', 'state', 'update_time', 'create_time'];

        fields.forEach(field => {
            if (debars.indexOf(field.COLUMN_NAME) < 0) {
                variables.push({
                    jName: self.$str.toCamel(field.COLUMN_NAME),
                    sName: field.COLUMN_NAME
                });
            }
        });

        const tmp = {
            tableName: name,
            variables: variables
        };

        return ejs.render(tmpFile, tmp);
    },
    selectPageTotal(self, name, fields){
        const tmpFile = this.readTmpFile(self, 'selectPageTotal');
        if (!tmpFile) return;

        let variables = [];

        const debars = ['id', 'state', 'update_time', 'create_time'];

        fields.forEach(field => {
            if (debars.indexOf(field.COLUMN_NAME) < 0) {
                variables.push({
                    jName: self.$str.toCamel(field.COLUMN_NAME),
                    sName: field.COLUMN_NAME
                });
            }
        });

        const tmp = {
            tableName: name,
            variables: variables
        };

        return ejs.render(tmpFile, tmp);
    },
    selectByKeyword(self, name, keyword){
        const tmpFile = this.readTmpFile(self, 'selectByKeyword');
        if (!tmpFile) return;

        const tmp = {
            tableName: name,
            sKeyword: keyword,
            jKeyword: self.$str.toCamel(keyword)
        };

        return ejs.render(tmpFile, tmp);
    },
    selectTotalByKeyword(self, name, keyword){
        const tmpFile = this.readTmpFile(self, 'selectTotalByKeyword');
        if (!tmpFile) return;

        const tmp = {
            tableName: name,
            sKeyword: keyword,
            jKeyword: self.$str.toCamel(keyword)
        };

        return ejs.render(tmpFile, tmp);
    }
}